package src;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TransactionQueryDialog extends JDialog {
    private JTable transactionTable;
    private DefaultTableModel tableModel;
    private JTextField searchField;
    private JComboBox<String> searchTypeCombo;
    private JTextField startDateField;
    private JTextField endDateField;

    public TransactionQueryDialog(Frame owner) {
        super(owner, "交易查询", true);
        setSize(1000, 600);
        setLocationRelativeTo(owner);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建搜索面板
        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        searchTypeCombo = new JComboBox<>(new String[]{"卡号", "交易类型"});
        searchField = new JTextField(20);
        JButton searchButton = new JButton("搜索");
        JButton resetButton = new JButton("重置");

        // 添加日期范围选择
        JLabel startDateLabel = new JLabel("开始日期:");
        startDateField = new JTextField(10);
        startDateField.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        
        JLabel endDateLabel = new JLabel("结束日期:");
        endDateField = new JTextField(10);
        endDateField.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

        searchPanel.add(new JLabel("搜索类型:"));
        searchPanel.add(searchTypeCombo);
        searchPanel.add(new JLabel("搜索内容:"));
        searchPanel.add(searchField);
        searchPanel.add(startDateLabel);
        searchPanel.add(startDateField);
        searchPanel.add(endDateLabel);
        searchPanel.add(endDateField);
        searchPanel.add(searchButton);
        searchPanel.add(resetButton);

        // 创建表格
        String[] columnNames = {"交易时间", "交易类型", "交易金额", "卡号", "备注"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        transactionTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(transactionTable);

        // 添加事件监听器
        searchButton.addActionListener(e -> performSearch());
        resetButton.addActionListener(e -> {
            searchField.setText("");
            startDateField.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            endDateField.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            refreshTable();
        });

        // 添加组件到主面板
        mainPanel.add(searchPanel, BorderLayout.NORTH);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 添加关闭按钮
        JButton closeButton = new JButton("关闭");
        closeButton.addActionListener(e -> dispose());
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(closeButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);

        // 初始加载数据
        refreshTable();
    }

    private void performSearch() {
        String searchType = (String) searchTypeCombo.getSelectedItem();
        String searchContent = searchField.getText().trim();
        String startDate = startDateField.getText().trim();
        String endDate = endDateField.getText().trim();
        
        // TODO: 实现实际的搜索逻辑
        // 这里只是示例，实际应该从数据库查询
        tableModel.setRowCount(0);
        if (!searchContent.isEmpty()) {
            // 模拟搜索结果
            Object[] row = {
                "2024-03-20 10:30:00",
                "存款",
                "1000.00",
                "6222021234567890",
                "柜台存款"
            };
            tableModel.addRow(row);
        }
    }

    private void refreshTable() {
        // TODO: 实现从数据库加载所有交易数据
        tableModel.setRowCount(0);
        // 模拟数据
        Object[][] data = {
            {"2024-03-20 10:30:00", "存款", "1000.00", "6222021234567890", "柜台存款"},
            {"2024-03-20 11:15:00", "取款", "500.00", "6222021234567890", "ATM取款"},
            {"2024-03-20 14:20:00", "转账", "200.00", "6222021234567890", "转账给6222021234567891"}
        };
        for (Object[] row : data) {
            tableModel.addRow(row);
        }
    }
} 